package producer.controller;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("/producer")
public class producerController {

    @Value("${server.port}")
    String port;

    @RequestMapping("/produce")
    public String produce() {
        return "生产者: " + port;
    }


    public static void main(String[] args) throws Exception {
        producerController p = new producerController();
//        p.producer(defaultMQProducer());
        p.sendAsynchronous(defaultMQProducer());
    }

    //同步发送
    public void producer(DefaultMQProducer producer) throws Exception {

        for (int i = 0; i < 10; i++) {
            // 创建消息，并指定Topic，Tag和消息体
            Message msg = new Message("TopicTest" /* Topic */,
                    "TagA" /* Tag */,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
            // 发送消息到一个Broker
            SendResult sendResult = producer.send(msg);
            // 通过sendResult返回消息是否成功送达
            System.out.printf("%s%n", sendResult);
        }
        // 如果不再发送消息，关闭Producer实例。
        producer.shutdown();

    }

    //异步发送
    public void sendAsynchronous(DefaultMQProducer producer) throws Exception {
        for (int i = 0; i < 10; i++) {
            final int index=i;
            // 创建消息，并指定Topic，Tag和消息体
            Message msg = new Message("TopicTest", "TagB", "OrderId888", "Helloworld".getBytes(RemotingHelper.DEFAULT_CHARSET));
            //SendCallback 接受异步回调的结果
            producer.send(msg, new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println("sendResult = " + sendResult);
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println("throwable = "+index + throwable);
                    throwable.printStackTrace();
                }
            });
        }
        Thread.sleep(5000);
        producer.shutdown();
    }
    //单项发送
    public void oneSend(DefaultMQProducer producer) throws Exception {
        //日志

        for (int i = 0; i < 10; i++) {
            // 创建消息，并指定Topic，Tag和消息体
            Message msg = new Message("TopicTest" /* Topic */,
                    "TagA" /* Tag */,
                    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
            );
        producer.sendOneway(msg);//
        }
        // 如果不再发送消息，关闭Producer实例。
        producer.shutdown();

    }

    public static DefaultMQProducer defaultMQProducer() throws MQClientException {
        // 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
        // 设置NameServer的地址
        producer.setNamesrvAddr("localhost:9876");
        // 设置消息同步发送失败时的重试次数，默认为 2
        producer.setRetryTimesWhenSendFailed(2);
        // 设置消息发送超时时间，默认3000ms
        producer.setSendMsgTimeout(3000);
        // 启动Producer实例
        producer.start();
        return producer;
    }




}
